<template>
  <view class="cart" v-if="cart.length">
    <!-- 收货地址 -->
    <my-address></my-address>
    <!-- 购物车商品列表的标题区域 -->
    <view class="cart-title">
      <!-- 左侧的图标 -->
      <uni-icons type="shop" size="18"></uni-icons>
      <!-- 描述文本 -->
      <text class="cart-title-text">购物车</text>
    </view>
    <!-- 商品列表区域 -->
    <!-- uni-swipe-action 是最外层包裹性质的容器 -->
    <uni-swipe-action>
      <block v-for="goods in cart" :key="goods.goods_id">
        <!-- uni-swipe-action-item 可以为其子节点提供滑动操作的效果。需要通过 options 属性来指定操作按钮的配置信息 -->
        <uni-swipe-action-item :right-options="options" @click="swipeActionClickHandler(goods.goods_id, $event)">
          <my-goods-item show-radio show-num :goods="goods" @on-radio-change="radioChangeHandler" @on-count-change="countChangeHandler"></my-goods-item>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>
    <!-- 结算 -->
    <my-settle @on-radio-change="allRadioChangeHandler"></my-settle>
  </view>
  <!-- 空白购物车区域 -->
  <view class="empty-cart" v-else>
    <navigator class="tip" open-type="switchTab" url="/pages/home/home">
      <image src="/static/cart_empty@2x.png" class="empty-img"></image>
      <!-- 去往首页选择新的商品添加到购物车中 -->
      <text class="tip-text">去逛逛</text>
    </navigator>
  </view>
</template>

<script>
import badgeMixin from '../../mixins/tabbar-badge.js';
import { mapState, mapActions } from 'vuex';
export default {
  mixins: [badgeMixin],
  data() {
    return {
      options: [
        {
          text: '删除', // 显示的文本内容
          style: {
            backgroundColor: '#C00000' // 按钮的背景颜色
          }
        }
      ]
    };
  },
  computed: {
    ...mapState('cart', ['cart'])
  },
  methods: {
    // 映射方法
    ...mapActions('cart', ['asyncUpdateGoodsState', 'asyncUpdateGoodsCount', 'asyncDelGoods', 'asyncUpdateAllGoodsState']),
    // 点击单选框组件实现选中状态的修改
    radioChangeHandler(goods_id) {
      this.asyncUpdateGoodsState(goods_id);
    },
    // 点击全选按钮修改所有的商品的选中状态
    async allRadioChangeHandler(goods_state) {
      await this.asyncUpdateAllGoodsState(goods_state);
    },
    // 点击uni-number-box时改变购物车的数量
    async countChangeHandler(goods) {
      await this.asyncUpdateGoodsCount(goods);
      // 手动设置tabbar的数字徽标
      // this.setTabBarBadge();
    },
    // 点击滑块按钮触发的事件
    async swipeActionClickHandler(goods_id, e) {
      if (e.content.text === '删除') {
        // 删除购物车中该数据
        await this.asyncDelGoods(goods_id);
        // 手动设置tabbar的数字徽标
        // this.setTabBarBadge();
      }
    }
  }
};
</script>

<style lang="scss">
.cart {
  // 为了内容不被底部的结算页面挡住
  padding-bottom: 50px;
  // 标题
  .cart-title {
    height: 40px;
    display: flex;
    align-items: center;
    font-size: 14px;
    padding-left: 5px;
    border-bottom: 1px solid #efefef;
    .cart-title-text {
      margin-left: 10px;
    }
  }
}
.empty-cart {
  padding-top: 150px;
  .tip {
    display: flex;
    flex-direction: column;
    align-items: center;
    .empty-img {
      width: 90px;
      height: 90px;
    }

    .tip-text {
      font-size: 16px;
      color: gray;
      margin-top: 15px;
    }
  }
}
</style>
